/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Apoiar;
import Util.ComentarioUtil;
import Model.Comentario;
import Model.Comentarios;
import Model.DetalhesPost;
import Model.PostInterface;
import Util.ApoiarUtil;
import Util.PostUtil;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.event.map.OverlaySelectEvent;
import org.primefaces.model.map.Marker;

@ManagedBean(name = "commentBean")
@SessionScoped
public class CommentBean implements Serializable {

    Comentario cm = new Comentario();
    private List<Comentarios> commnets;
    public String codPost;
    private int codUsuario;
    private String comentario;
    private Marker marker;
    public String descricao;
    public String titulo;
    private String dataPost;
    private int cod;
    private long apoios;
    private boolean buttonStatus;
    
    HttpSession session = (HttpSession) FacesContext
            .getCurrentInstance().getExternalContext().getSession(true);

    public void refreshIndex() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");

    }

    public boolean isButtonStatus() {
        return buttonStatus;
    }

    public void setButtonStatus(boolean buttonStatus) {
        this.buttonStatus = buttonStatus;
    }

    public long getApoios() {
        return apoios;
    }

    public void setApoios(long apoios) {
        this.apoios = apoios;
    }
    

   

    
    
    public String getDataPost() {
        return dataPost;
    }

    public void setDataPost(String dataPost) {
        this.dataPost = dataPost;
    }

    public int getCod() {
        return cod;
    }

    public void setCod(int cod) {
        this.cod = cod;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public String getCodPost() {
        return codPost;
    }

    public void setCodPost(String codPost) {
        this.codPost = codPost;
    }

    public int getCodUsuario() {
        return codUsuario;
    }

    public void setCodUsuario(int codUsuario) {
        this.codUsuario = codUsuario;
    }

    public String getComentario() {
        return comentario;
    }

    public void setComentario(String comentario) {
        this.comentario = comentario;
    }

    public Marker getMarker() {
        return marker;
    }

    public List<Comentarios> getComments() {
        return commnets;
    }

    public CommentBean() {
        commnets = new ArrayList<Comentarios>();
       //  commnets = ComentarioUtil.getComentarios(marker.getShadow());
        
    }

    public void onMarkerSelect(OverlaySelectEvent event) {
               
        ArrayList<PostInterface> posts;
        DetalhesPost dp= new DetalhesPost();
        PostInterface post = new PostInterface();
        Apoiar ap = new Apoiar();

        marker = (Marker) event.getOverlay();
        commnets = ComentarioUtil.getComentarios(marker.getShadow());
       
        posts = MapBean.post;
        for (int i = 0; i < posts.size(); i++) {
            if (marker.getShadow().equals(posts.get(i).getCod().toString())) {
                descricao = posts.get(i).getDescricao();
                titulo = posts.get(i).getTitulo();
                dataPost = posts.get(i).getData();
                cod = posts.get(i).getCod();
                if((String) session.getAttribute("cod") != null){
                ap.setCodUsuario(Integer.parseInt( (String) session.getAttribute("cod")));
                ap.setCodPost(Integer.parseInt(marker.getShadow()));
                dp=ApoiarUtil.getApoiar(ap);
                apoios=dp.getQtdeApoios();
                if(dp.getApoiou()==1){
                    buttonStatus=true;
                }
                else{
                    buttonStatus=false;
                }
                }
            }

        }

        
    }

    public void create() {
        String userCod = (String) session.getAttribute("cod");
        ComentarioUtil.sendComment(codPost, Integer.parseInt(userCod), comentario);
    }
    
    
    
    public void carregaComentario() {
        if(marker.getShadow()!=null)
        commnets = ComentarioUtil.getComentarios(marker.getShadow());
      }

    public void deletaComentario() {

        cm.setCod(Integer.parseInt(codPost));
        ComentarioUtil.deletaComentario(cm);

    }

    public void deletaPost() throws IOException {
        PostInterface post = new PostInterface();
        post.setCod(cod);
        PostUtil.deletaPost(post);
        refreshIndex();




    }
}
